package com.servlet.listener;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebListener;

/**
 * @Title: MyRequestListener
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/26 11:12
 * 监听request 对象的
 */
@WebListener
public class MyRequestListener implements ServletRequestListener {

    /**
     * 在请求servlet 之前启动的
     * @param servletRequestEvent
     */
    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        ServletRequest request = servletRequestEvent.getServletRequest();
        Object user = request.getAttribute("userrequest");
        System.out.println("创建:"+user); // null
        System.out.println("request 请求被创建....");

    }

    /**
     * request 一次请求之后自动销毁
     * @param servletRequestEvent
     */
    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
        ServletRequest request = servletRequestEvent.getServletRequest();
        Object user = request.getAttribute("userrequest");
        System.out.println("销毁:"+user);
        System.out.println("request 被销毁....");
    }


}
